import { defineConfig, loadEnv } from "vite";
import vue from "@vitejs/plugin-vue";
import AutoImport from "unplugin-auto-import/vite";
import Components from "unplugin-vue-components/vite";
import { AntDesignVueResolver } from "unplugin-vue-components/resolvers";
import path from "path";

export default defineConfig(({ mode }) => {
  const env = loadEnv(mode, process.cwd());
  return {
    plugins: [
      vue(),
      Components({
        resolvers: [
          AntDesignVueResolver({ importStyle: false, resolveIcons: true }),
        ],
      }),
      AutoImport({
        resolvers: [AntDesignVueResolver()],
        imports: ["vue", "vue-router"],
        dts: "src/auto-import.d.ts",
      }),
    ],
    resolve: {
      alias: {
        "@": path.resolve(__dirname, "./src"),
      },
    },
    server: {
      hmr: true,
      host: "0.0.0.0",
      origin: "http://localhost:5173",
      port: 5173,
      proxy: {
        [env.VITE_APP_BASE_API]: {
          target: env.VITE_APP_BASE_SERVER,
          secure: false,
          changeOrigin: true, // 开启代理，在本地创建一个虚拟服务端
          rewrite: (path) => path.replace(/^\/api/, ""),
          // bypass(req, res, options) {
          //     res.setHeader("A-Real-Url", options.target);
          // }
        },
      },
    },
  };
});
